package com.test.mchelepesa.adapter_rice;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.mchelepesa.R;
import com.test.mchelepesa.bean_rice.config_info_rice.AppShowInfo_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class AccountChannelAdapter extends RecyclerView.Adapter<AccountChannelAdapter.AccountChannelHolder> {

    private final ArrayList<AppShowInfo_rice.WalletChannel> channelInfoList;

    private AppShowInfo_rice.WalletChannel selectChannel;

    private final ArrayList<AccountChannelHolder> holderList = new ArrayList<>();

    public AccountChannelAdapter(ArrayList<AppShowInfo_rice.WalletChannel> channelInfoList){
        this.channelInfoList = channelInfoList;
    }

    @NonNull
    @Override
    public AccountChannelHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AccountChannelHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_account_channel,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull AccountChannelHolder holder, int position) {

        AppShowInfo_rice.WalletChannel channelInfo = channelInfoList.get(position);

        String logoUrl = channelInfo.getLogo();
        if (!TextUtils.isEmpty(logoUrl)){
            new Thread(() -> {
                try {
                    URL url = new URL(logoUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(4995);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        new Handler(Looper.getMainLooper()).post(() -> holder.logoIv.setImageBitmap(bitmap));
                    }
                }catch (Exception e){
                    StringUtil_rice.printException(e);
                }
            }).start();
        }

        holder.stChannelDesc(channelInfo.getDesc());

        if (!holderList.contains(holder)) holderList.add(holder);

        holder.stClick(v -> {
            selectChannel = channelInfo;
            for (int i=0;i<holderList.size();i++){
                holderList.get(i).stCheck(i==position);
            }
        });


    }

    public String getSelectChannelValue(){
        if (selectChannel == null) return "";
        return selectChannel.getValue();
    }

    public String getSelectChannelDesc(){
        if (selectChannel == null) return "";
        return selectChannel.getDesc();
    }

    @Override
    public int getItemCount() {
        return channelInfoList.size();
    }

    public static class AccountChannelHolder extends RecyclerView.ViewHolder{

        private final View channelView;
        private final ImageView logoIv;
        private final TextView channelTv;
        private final View checkIv;

        private final int SelectColor = Color.parseColor("#FFFC724D");
        private final int UnselectColor = Color.parseColor("#FFB1B5B9");

        @SuppressLint("InflateParams")
        public AccountChannelHolder(View view){

            super(view);

            channelView = view.findViewById(R.id.item_account_channel_view_channel);
            logoIv = view.findViewById(R.id.item_account_channel_iv_logo);
            channelTv = view.findViewById(R.id.item_account_channel_tv_channel);
            checkIv = view.findViewById(R.id.item_account_channel_view_check);

        }

        public void stClick(View.OnClickListener clickListener){
            if (channelView !=null) channelView.setOnClickListener(clickListener);
        }

        public void stCheck(boolean isCheck){
            if (isCheck){
                HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_ADD_ACCOUNT_TYPE_CLICK", StringUtil_rice.getSafeString(channelTv.getText().toString()));
                channelView.setBackgroundResource(R.drawable.bg_account_channel_select);
                channelTv.setTextColor(SelectColor);
                checkIv.setBackgroundResource(R.drawable.icon_select);
            }else {
                channelView.setBackgroundResource(R.drawable.bg_account_channel_unselect);
                channelTv.setTextColor(UnselectColor);
                checkIv.setBackgroundResource(R.drawable.icon_unselect);
            }
        }

        public void stChannelDesc(String text){
            channelTv.setText(text);
        }

    }

}
